/**
 * FileName: HttpRequestLocal
 * Author:   Leopold
 * Date:     2018/9/11 15:02
 * Description:
 * History:
 */
package cn.mcsys.viewer.core.utils;

import cn.mcsys.myadmin.modules.air.entity.AirRidePoint;
import cn.mcsys.myadmin.modules.air.entity.AirWorker;
import cn.mcsys.myadmin.modules.viewer.entity.AirViewerUser;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 保留用户会话，以方便在业务代码任何地方调用
 * {@link }
 *
 * @author lijiazhi
 */
@Component
public class HttpRequestLocal {

    public static final String USER_SESSION = "userinfo";

    public static final String WORK_SESSION = "workinfo";

    private static final HttpRequestLocal HTTP_REQUEST_LOCAL = new HttpRequestLocal();

    private HttpRequestLocal() {

    }

    public static HttpRequestLocal getHttpRequestLocal() {
        return HTTP_REQUEST_LOCAL;
    }

    private static final ThreadLocal<HttpServletRequest> requests =
            new ThreadLocal<HttpServletRequest>() {
                @Override
                protected HttpServletRequest initialValue() {
                    return null;
                }
            };

    public Object getRequestValue(String attr) {
        return requests.get().getAttribute(attr);
    }

    public String getRequestURI() {
        return requests.get().getRequestURI();
    }

    public void set(HttpServletRequest request) {
        requests.set(request);
    }

    public static HttpSession getSession(){
        ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attr.getRequest().getSession();
    }

    //设置User到session到中
    public static void setUserSession(AirViewerUser user) {
        HttpSession session = getSession();
        session.setAttribute(USER_SESSION, user);
    }

    public static AirViewerUser getUserSession() {
        HttpSession session = getSession();
        AirViewerUser user = (AirViewerUser) session.getAttribute(USER_SESSION);
//        if (user == null) {
//            user = new AirViewerUser();
//            user.setId(40L);
//            user.setNickname("测试用户1");
//            user.setOpenid("sadasfxzcvasdfasdfasdfasdfaa");
//        }
        return user;
    }


    public Object getSessionValue(String attr) {
        return requests.get().getSession().getAttribute(attr);
    }

    public void setSessionValue(String attr, Object obj) {
        requests.get().getSession().setAttribute(attr, obj);
    }

    //设置User到session到中
    public static void setWorkUserSession(AirWorker user) {
        getSession().setAttribute(WORK_SESSION, user);
    }

    public static AirWorker getWorkUserSession() {
        AirWorker user = (AirWorker) getSession().getAttribute(WORK_SESSION);
//        if (user == null) {
//            user = new AirWorker();
//            user.setId(3);
//            AirRidePoint airRidePoint = new AirRidePoint();
//            airRidePoint.setId(1);
//            airRidePoint.setName("香洲公交总站");
//            airRidePoint.setPosition("广东省珠海市香洲区紫荆路142号");
//            user.setAirRidePoint(airRidePoint);
//            user.setName("工作人员测试用户1");
//            user.setBelongUnit("sadasfxzcvasdfasdfasdfasdfaa");
//        }
        return user;
    }

}
